package com.quanyan.signup.web;

import com.alibaba.fastjson.JSON;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.signup.entity.vo.req.ReqExpertMsg;
import com.quanyan.signup.entity.vo.req.ReqSerchReferUser;
import com.quanyan.signup.entity.vo.req.ReqSignUpUser;
import com.quanyan.signup.entity.vo.resp.RespExpertMsg;
import com.quanyan.signup.entity.vo.resp.RespSerchUser;
import com.quanyan.signup.service.SignUpService;
import com.quanyan.sms.service.SmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sms.SmsApi;
import sms.model.Result;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/7/6.
 */
@RestController
@RequestMapping(value = "api/signup")
public class SignUpController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SignUpController.class);

    @Autowired
    private SmsService smsService;

    @Autowired
    private SignUpService signUpService;

    /**
     * 发送短信
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "sendSmsToUserSign",method = RequestMethod.POST)
    public APIResponse<Result> sendSmsToUserSign(@RequestBody Map<String,Object> paramMap){
        LOGGER.debug("--------sendSmsToUserSign------begin-----");
        Result rs=smsService.sendValidCodetToPhone((String) paramMap.get("mobile"), SmsApi.MESSAGE_TEMPLATE_USER_REGISTER,60,120);
        LOGGER.debug("-------短信发送返回结果---------"+JSON.toJSONString(rs));
        return APIResponse.returnSuccess(rs);
    }

    /**
     * 验证短信
     * @return
     */
    @RequestMapping(value = "verifySmsToUserSign",method = RequestMethod.POST)
    public APIResponse<Boolean> verifySmsToUserSign(@RequestBody Map<String,Object> paramMap){
        LOGGER.debug("--------verifySmsToUserSign------begin-----");
        boolean validFlag=smsService.verifyValidCode((String)paramMap.get("mobile"), SmsApi.MESSAGE_TEMPLATE_USER_REGISTER,(String)paramMap.get("validSms"));
        LOGGER.debug("-------------短信验证成功失败------------------>"+validFlag);
        if(validFlag){
            return APIResponse.returnSuccess(validFlag);
        }else {
            return  APIResponse.returnFail("验证失败");
        }
    }

    /**
     * 报名成功发送短信
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "noticeSmsToUser",method = RequestMethod.POST)
    public APIResponse<Result> noticeSmsToUser(@RequestBody Map<String,Object> paramMap){
        LOGGER.debug("--------sendSmsToUserSign------begin-----");
        Result rs=smsService.sendMessageToPhone((String) paramMap.get("mobile"), SmsApi.MESSAGE_TEMPLATE_SIGNUP_SMS,new String[]{"百灵鸟运动达人造星招募活动"},true);
        LOGGER.debug("-------短信发送返回结果---------"+JSON.toJSONString(rs));
        return APIResponse.returnSuccess(rs);
    }

   /* *//**
     * 获取城市区域信息
     * @return
     *//*
    @RequestMapping(value = "getAllCityDsitrict",method = RequestMethod.POST)
    public APIResponse<List<RespCityDistrict>> getAllCityDsitrict(){
        LOGGER.debug("--------getAllCityDsitrict------begin-----");
        return signUpService.getCityDistrict();
    }*/

    /**
     * 用户报名
     * @return
     */
    @RequestMapping(value = "userSign",method = RequestMethod.POST)
    public APIResponse userSign(@RequestBody ReqSignUpUser reqSignUpUser){
        LOGGER.debug("--------userSign------begin-----");
        return signUpService.signUpUser(reqSignUpUser);
    }

    /**
     * 用户报名第二步
     * @return
     */
    @RequestMapping(value = "userSignToNext",method = RequestMethod.POST)
    public APIResponse userSignToNext(@RequestBody Map<String,Object> paramMap){
        LOGGER.debug("--------userSignToNext------begin-----");
        return signUpService.signUpUserToNext(paramMap);
    }

    /**
     * 查看推荐人数量
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "referUserNumber",method = RequestMethod.POST)
    public APIResponse referUserNumber(@RequestBody Map<String,Object> paramMap) {
        LOGGER.debug("--------referUserNumber------begin-----");
        return signUpService.referUserNumber((String) paramMap.get("mobile"));
    }

    /**
     * 查询推荐人信息
     * @param reqSerchReferUser
     * @return
     */
    @RequestMapping(value = "searchReferUser",method = RequestMethod.POST)
    public APIResponse<List<RespSerchUser>> searchReferUser(@RequestBody ReqSerchReferUser reqSerchReferUser) {
        LOGGER.debug("--------searchReferUser------begin-----");
        return signUpService.searchReferUser(reqSerchReferUser);
    }

    /**
     * 微信达人发送手机号查看状态
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "queryUserStatuByPhone",method = RequestMethod.POST)
    public String queryUserStatuByPhone(@RequestBody Map<String,Object> paramMap) {
        LOGGER.debug("--------queryUserStatuByPhone------begin-----");
        String mobile= (String) paramMap.get("mobile");
        if (mobile==null){
            return "手机号不能为空";
        }
        return signUpService.queryUserStatuByPhone(mobile).getData();
    }

    /**
     * 根据手机号查询达人信息，修改
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "queryExpertMsgByMobile",method = RequestMethod.POST)
    public APIResponse<RespExpertMsg> queryExpertMsgByMobile(@RequestBody Map<String,Object> paramMap) {
        LOGGER.debug("--------queryExpertMsgByMobile------begin-----");
        if(null != paramMap){
            String mobile= (String) paramMap.get("mobile");
            return signUpService.queryExpertMsgByMobile(mobile);
        }
        return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
    }

    /**
     * 根据手机号，跟新部分达人信息
     * @param reqExpertMsg
     * @return
     */
    @RequestMapping(value = "updateExpertMsgByMobile",method = RequestMethod.POST)
    public APIResponse<RespExpertMsg> updateExpertMsgByMobile(@RequestBody ReqExpertMsg reqExpertMsg) {
        LOGGER.debug("--------updateExpertMsgByMobile------begin-----");
        if(null != reqExpertMsg){
            return signUpService.updateExpertMsgByMobile(reqExpertMsg);
        }
        return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
    }

    /**
     * 检查手机号是否是达人
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "checkExpertMobile",method = RequestMethod.POST)
    public APIResponse<RespExpertMsg> checkExpertMobile(@RequestBody Map<String,Object> paramMap) {
        LOGGER.debug("--------checkExpertMobile------begin-----");
        if(null != paramMap){
            return signUpService.checkExpertMobile((String) paramMap.get("mobile"));
        }
        return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
    }

}
